<style>

</style>

<template>
  <p>
    <a-space>
        <TrainSelectView v-model:value="queryParam.trainCode" width="200px"></TrainSelectView>
        <a-date-picker v-model:value="queryParam.date" valueFormat="YYYY-MM-DD" placeholder="请选择日期" />
        <a-button type="primary" @click="handleQuery()">查询</a-button>
    </a-space>
    </p>
    <a-table 
        :dataSource="dailyTrainSeats" 
        :columns="columns" 
        :pagination="pagination" 
        :loading="loading"
        @change="handleTableChange" 
    >
    </a-table>
    <a-modal v-model:visible="visible" title="火车座位" @ok="handleOk" ok-text="确认" cancel-text="取消">
        <a-form :model="dailyTrainSeat" :label-col="{span: 4}" :wrapper-col="{span: 14}">
            <a-form-item label="日期">
                <a-date-picker v-model:value="dailyTrainSeat.date" valueFormat="YYYY-MM-DD" placeholder="请选择日期" :disabled="!!dailyTrainSeat.id" />
            </a-form-item>
            <a-form-item label="车次编号">
                <TrainSelectView v-model:value="dailyTrainSeat.trainCode"></TrainSelectView>
            </a-form-item>
            <a-form-item label="箱序">
                <a-input v-model:value="dailyTrainSeat.carriageIndex"/>
            </a-form-item>
            <a-form-item label="排号">
                <a-input v-model:value="dailyTrainSeat.seatRow"/>
            </a-form-item>
            <a-form-item label="列号">
              <a-input v-model:value="dailyTrainSeat.seatCol"/>
            </a-form-item>
            <a-form-item label="座位类型">
                <a-select v-model:value="dailyTrainSeat.seatType">
                    <a-select-option value="A">A</a-select-option>
                    <a-select-option value="C">C</a-select-option>
                    <a-select-option value="D">D</a-select-option>
                    <a-select-option value="F">F</a-select-option>
                    <a-select-option value="A">A</a-select-option>
                    <a-select-option value="B">B</a-select-option>
                    <a-select-option value="C">C</a-select-option>
                    <a-select-option value="D">D</a-select-option>
                    <a-select-option value="F">F</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="同车厢座序">
              <a-input v-model:value="dailyTrainSeat.carriageSeatIndex"/>
            </a-form-item>
            <a-form-item label="售卖情况">
              <a-input v-model:value="dailyTrainSeat.sell"/>
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script>
import { defineComponent,ref,onMounted} from 'vue';
import { notification } from 'ant-design-vue';
import axios from 'axios';
import TrainSelectView from '@/components/TrainSelectView.vue';

export default defineComponent({
  name: "dailyTrainSeat-view",
  components: { TrainSelectView },
  setup() {
    // 展示模态窗的标签的定义
    const visible = ref(false);
    const loading = ref(false);
    const queryParam = ref({
        trainCode: null,
        date: null
    });

    // 定义车站对象
    let dailyTrainSeat = ref({
        id: undefined,
        date:undefined,
        trainCode: undefined,
        carriageIndex: undefined,
        seatRow: undefined,
        seatCol: undefined,
        seatType: undefined,
        carriageSeatIndex:undefined,
        sell:undefined,
        createTime: undefined,
        updateTime: undefined
    });

    const dailyTrainSeats = ref([]);
    
    const columns = [
        {
            title: '日期',
            dataIndex: 'date',
            key: 'date',
        },
        {
            title: '车次编号',
            dataIndex: 'trainCode',
            key: 'trainCode',
        },
        {
            title: '厢序',
            dataIndex: 'carriageIndex',
            key: 'carriageIndex',
        },
        {
            title: '排号',
            dataIndex: 'seatRow',
            key: 'seatRow',
        },
        {
            title: '列号',
            dataIndex: 'seatCol',
            key: 'seatCol',
        },
        {
            title: '座位类型',
            dataIndex: 'seatType',
            key: 'seatType',
        },
        {
            title: '同车厢座序',
            dataIndex: 'carriageSeatIndex',
            key: 'carriageSeatIndex',
        },
        {
            title: '售卖情况',
            dataIndex: 'sell',
            key: 'sell',
        },
    ];

    let pagination =ref({
        total: 0,
        current: 1,
        pageSize: 10,
    });

    // 车站信息列表查询的方法
    const handleQuery = e => {
        // 刷新时候不带参数的判断
        if(!e){
            e = {
                page: 1,
                limit: pagination.value.pageSize
            }
        }
        // 开启loading效果
        loading.value = true
        axios.get("/business/admin/daily-train-seat/query",
            {
                params:{
                    page: e.page,
                    limit: e.limit,
                    trainCode: queryParam.value.trainCode,
                    date: queryParam.value.date
                }
            })
            .then( response => {
                // 关闭loading效果
                loading.value = false
                let respData = response.data
                let trainSeatDataList = respData.data.dataList
                 // 对车厢座位类型进行转换
                 trainSeatDataList.forEach(dailyTrainSeat => {
                    switch (dailyTrainSeat.seatType) {
                        case "1":
                            dailyTrainSeat.seatType = "一等座"
                            break;
                        case "2":
                            dailyTrainSeat.seatType = "二等座"
                            break;
                        case "3":
                            dailyTrainSeat.seatType = "软卧"
                            break;
                        case "4":
                            dailyTrainSeat.seatType = "硬卧"
                            break;
                    }
                });
                // 分页控件的设置
                let total = respData.data.total
                let currentPage = respData.data.currentPage
                let pageSize = respData.data.pageSize
                dailyTrainSeats.value = trainSeatDataList
                pagination.value.current = currentPage
                pagination.value.pageSize = pageSize
                pagination.value.total = total
            })
            .catch( error => {
                // 关闭loading效果
                loading.value = false
                let errData = error.response.data
                console.log(errData)
                notification.error({ description: errData.data });
            })
    };

    // 页码的点击事件
    const handleTableChange = (pagination) => {
        console.log("pagination:",pagination.value)
        // 调用车站列表查询方法
        handleQuery({
            page: pagination.current,
            limit: pagination.pageSize
        })
    };
    // 钩子函数，页面初始化的时候执行
    onMounted(() => {
        handleQuery({
            page: pagination.value.current,
            limit: pagination.value.pageSize
        })
    });


    return {
        visible,
        loading,
        queryParam,
        dailyTrainSeat,
        dailyTrainSeats,
        columns,
        pagination,
        handleQuery,
        handleTableChange
    };
  },
});
</script>